const mysql = require('mysql');
const mysqlConfig = require('../../../config/mysql.json');

let poolHandle;

async function getMySqlConnection () {
    if (!poolHandle) {
        poolHandle = mysql.createPool(mysqlConfig);
    }
    return poolHandle;
}

async function makeQuery (queryStatement) {
    let handle = await getMySqlConnection();
    return new Promise((resolve, reject) => {
        handle.getConnection((err, conn) => {
            if (err) {
                reject(err);
            } else {
                conn.query(queryStatement, (err, result) => {
                    conn.release();
                    if (err) {
                        reject(err);
                    } else {
                        resolve(result);
                    }
                });
            }
        });
    });
}

module.exports = {
    makeQuery
};
